import getData from '../../service/getData.js'
import getData2 from '../../service/getData2.js'
import * as types from '../types'
import * as types1 from '../appendType'

export const info = {
    state:{
        infoList:[],//消息列表

        pendingComtInfo:[],//待评价
        pendingPage:1,//分页
        pendingPageLimit:10,

        /**
         设置评价内容
         */
        setPendingInfo:{},
//消息详情

//  电子券消息详情&&订单消息
        transaction:[],
        transactionPage:1,
        transactionLimit:8,
//  券友消息列表
        friendList:[],
        friendPage:1,
        friendPageLimit:15,
        //过期提醒
        expiredList:[],
        expiredPage:1,
        expiredPageLimit:6,
        //系统消息
        systemList:[],
        systemPage:1,
        systemPageLimit:8,
    },
    actions:{
        //我的消息
        getMyListInfo:function({state, commit, rootState,getters},params){
            return new Promise((resolve, reject) => {
                getData.myInfoList(params,getters.userNum).then(function (rep) {
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types.MY_INFOLIST,rep)
                    }
                }).catch(rep => {
                    reject(rep);
                })
            })
        },
        //待评价
        pendingComt:function({state, commit, rootState,getters},params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.pendingPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.pendingPageLimit+"}]"
            };
            params.data = params.data || {};
            params.data = Object.assign(page,params.data);

            return new Promise((resolve, reject) => {
                getData.pendingComt(params,getters.userNum).then(function (rep) {
                    //console.log(rep)
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types.pendingInfo,rep);
                        if(rep.content.couponComList.length == 0) {
                            return;
                        }
                        state.pendingPage++;
                    }
                }).catch(rep => {
                    reject(rep);
                })
            })
        },
        //电子券消息详情&&订单消息
        getInfoList:function({state, commit, rootState,getters},params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.transactionPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.transactionLimit+"}]"
            };

            params.data = params.data || {};
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                getData.myClassInfo(params, getters.userNum).then(function (rep) {
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types.transaction_info,rep);
                        if(rep.content.list.length == 0) {
                            return;
                        }
                        state.transactionPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                })
            })
        },
//  券友消息列表
        newFriend:function({state, commit, rootState,getters},params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.friendPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.friendPageLimit+"}]"
            };

            params.data = params.data || {};
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData.newFriend(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);
                        if(rep.respCode == "00"){
                            commit(types.friendList,rep);
                            if(rep.content.list.length == 0) {
                                return;
                            }
                            state.friendPage++;
                        }
                    }).catch(function(rep){
                        reject(rep)
                    });
                }
            })
        },
        //过期提醒列表
        expiredItem:function({state,commit,rootState,getters },params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.expiredPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.expiredPageLimit+"}]"
            };
            params.data = Object.assign(page,params.data);

            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {

                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData2.expiredItem(params, getters.userNum).then(function (rep) {
                        //console.log(rep)
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);
                        if(rep.respCode == "00"){
                            commit(types1.EXPIRED,rep);
                            if(rep.content.couponList.length == 0) {
                                return;
                            }
                            state.expiredPage++;
                        }
                    }).catch(function(rep){
                        reject(rep)
                    });
                }
            });
        },
        //系统消息提醒
        systemList:function({state, commit, rootState,getters},params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.systemPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.systemPageLimit+"}]"
            };
            params.data = params.data || {};
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                getData2.systemList(params, getters.userNum).then(function (rep) {
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types.SYSTEM_INFO,rep);
                        if(rep.content.list.length == 0) {
                            return;
                        }
                        state.systemPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                })
            })
        }
        
    },
    getters:{

    },
    mutations:{
        /**
         * 我的消息
         */
            [types.MY_INFOLIST](state,rep){
            if(!rep){
                state.infoList = [];
                return
            }
            state.infoList = rep.content.msg;
        },
        /**
         * 待评价列表
         */
            //列表
            [types.pendingInfo](state,rep){
            if(!rep){
                state.pendingComtInfo = [];
                return
            }
            if (state.pendingPage <= 1) {
                state.pendingComtInfo = rep.content.couponComList
            } else {
                state.pendingComtInfo = state.pendingComtInfo.concat(rep.content.couponComList)
            }
        },
        //分页
        [types.pendingInfoPage](state,num){
            state.pendingPage = num;
        },
        /**
         * 设置待评论内容
         */
            //设置待评论内容
            [types.setPendingInfo](state,params){
            state.setPendingInfo = params ? params : {};
        },
        /**
         * 电子券消息详情&&订单消息
         */
            //列表
        [types.transaction_info](state,rep){
            if(!rep){
                state.transaction = [];
                return
            };
            for (var i = rep.content.list.length - 1; i >= 0; i--) {
                var index;
                // rep.content.list[i].content=;
                // console.log(rep.content.list[i].msgContent);
                // console.log(rep.content.list[i].msgContent.indexOf('折扣券'));
                if (rep.content.list[i].msgContent.lastIndexOf ('折扣券')>=0) {
                    index=rep.content.list[i].msgContent.lastIndexOf ('折扣券');
                     console.log(index)
                    rep.content.list[i].msgContent=rep.content.list[i].msgContent.substring(0,index)+'<span style="color:#77bc6b">折扣券</span>'+rep.content.list[i].msgContent.substring(index+3)
                }else if(rep.content.list[i].msgContent.lastIndexOf('代金券')>=0){
                    index=rep.content.list[i].msgContent.lastIndexOf('代金券');
                    rep.content.list[i].msgContent=rep.content.list[i].msgContent.substring(0,index)+'<span style="color:#f59e06">代金券</span>'+rep.content.list[i].msgContent.substring(index+3)
                }else if(rep.content.list[i].msgContent.lastIndexOf('抵用券')>=0){
                    index=rep.content.list[i].msgContent.lastIndexOf('抵用券');
                   
                    rep.content.list[i].msgContent=rep.content.list[i].msgContent.substring(0,index)+'<span style="color:#72afc4">抵用券</span>'+rep.content.list[i].msgContent.substring(index+3)
                }else if(rep.content.list[i].msgContent.lastIndexOf('礼品券')>=0 && rep.content.list[i].msgTiTle!=='发货消息'){
                    index=rep.content.list[i].msgContent.lastIndexOf('礼品券');
                    rep.content.list[i].msgContent=rep.content.list[i].msgContent.substring(0,index)+'<span style="color:#fe7e5c">礼品券</span>'+rep.content.list[i].msgContent.substring(index+3)
                }else if(rep.content.list[i].msgContent.lastIndexOf('票务')>=0 && rep.content.list[i].msgTiTle!=='发货消息'){
                    index=rep.content.list[i].msgContent.lastIndexOf('票务');
                    rep.content.list[i].msgContent=rep.content.list[i].msgContent.substring(0,index)+'<span style="color:#b790cd">票务</span>'+rep.content.list[i].msgContent.substring(index+2)
                }
                
            };
            if (state.transactionPage <= 1) {
                state.transaction = rep.content.list
            } else {
                state.transaction = state.transaction.concat(rep.content.list)
            }
        },
        //分页
        [types.transaction_page](state,num){
            state.transactionPage = num
        },
        /**
         * 券友圈
         */
        //列表
            [types.friendList](state,rep){
            if(!rep){
                state.friendList = [];
                return
            }
            if (state.friendPage <= 1) {
                state.friendList = rep.content.list
            } else {
                state.friendList = state.friendList.concat(rep.content.list)
            }
        },
        //分页
        [types.friendList_page](state,num){
            state.friendPage = num
        },

        // 过期提醒列表
        [types1.EXPIRED](state,rep){

            if(!rep){
                state.expiredList = [];
                return
            }

            if (state.expiredPage <= 1) {

                state.expiredList = rep.content.couponList;
            } else {
                state.expiredList = state.expiredList.concat(rep.content.couponList);
                
            }

        },
        //分页
        [types1.EXPIRED_PAGE](state,num){
            state.expiredPage = num;
        },
        // 系统消息列表
        [types.SYSTEM_INFO](state,rep){
            if(!rep){
                state.systemList = [];
                return
            };
            if (state.systemList <= 1) {
                state.systemList = rep.content.list
            } else {
                state.systemList = state.systemList.concat(rep.content.list)
            }
        },
        //分页
        [types.SYSTEM_PAGE](state,num){
            state.systemPage = num;
        },
    }

}